package com.zmn.plat.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * 类描述：
 *
 * @author hewenlang
 * @since 2022/01/06 19:10
 */
public class DataUtils {
    private static final String OVERLAY = "****";
    private static final int START = 3;
    private static final int END = 7;

    private static final Pattern MOBILE_PATTERN =
            Pattern.compile("^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|17[0-9]{9}$|18[0-9]{9}$|19[0-9]{9}$");
    private static Pattern isNumericPattern = Pattern.compile("^[-\\+]?[\\d]*$");

    /**
     * 验证是否为手机号
     * @param number
     * @return
     */
    public static boolean isMobile(String number) {
        return MOBILE_PATTERN.matcher(number).matches();
    }

    /**
     * 验证是否为银行卡号
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        return isNumericPattern.matcher(str).matches();
    }

    /**
     * 验证是否为邮箱
     * @param str
     * @return
     */
    public static boolean isEmail(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        String at = "@";
        if (!str.contains(at)) {
            return false;
        }
        return true;
    }

    /**
     * 139****0504
     *
     * @param content
     * @return
     */
    public static String maskMobile(String content) {
        if (StringUtils.isEmpty(content)) {
            return "";
        }
        return StringUtils.overlay(content, OVERLAY, START, END);
    }
    /**
     * 过滤邮箱账号
     * 132****99308084911
     *
     * @param email
     * @return
     */
    public static String maskEmail(String email) {
        String at = "@";
        /**
         * 这里主要逻辑是需要保留邮箱的注册商 比如@qq.com
         */
        int length = StringUtils.indexOf(email, at);
        String content = StringUtils.substring(email, 0, length);
        String mask = StringUtils.overlay(content, OVERLAY, START, END);
        return mask + StringUtils.substring(email, length);
    }

    /**
     * 身份证打码操作
     * 132****99308084911
     * @param idCard
     * @return
     */
    public static String maskIdCard(String idCard) {
        if (StringUtils.isEmpty(idCard)) {
            return "";
        }
        return StringUtils.overlay(idCard, OVERLAY, START, END);
    }

    /**
     * 支付宝账号处理
     * @param number
     * @return
     */
    public static String wxAccount(String number){
        if(StringUtils.isNotEmpty(number)) {
            if (isMobile(number)) {
                return maskMobile(number);
            } else if (isEmail(number)) {
                return maskEmail(number);
            } else {
                return StringUtils.overlay(number, OVERLAY, 4, END);
            }
        }
        return null;
    }
}
